//package com.hanlink.dsp.ui.fragment;
//
//import android.content.Context;
//import android.os.Bundle;
//import android.view.Gravity;
//import android.view.View;
//import android.widget.Button;
//import android.widget.ListView;
//import android.widget.ProgressBar;
//import android.widget.RadioGroup;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import androidx.annotation.NonNull;
//import androidx.appcompat.widget.Toolbar;
//import androidx.drawerlayout.widget.DrawerLayout;
//import androidx.lifecycle.ViewModelProvider;
//import androidx.lifecycle.ViewModelProviders;
//
//import com.hanlink.dsp.BtDspApplication;
//import com.hanlink.dsp.R;
//import com.hanlink.dsp.bluetooth.ble.BtDevice;
//import com.hanlink.dsp.protocols.DeviceProtocol;
//import com.hanlink.dsp.ui.activities.DViewModel;
//import com.hanlink.dsp.ui.adapter.DeviceAdapter;
//import com.hanlink.dsp.util.DialogHelper;
//import com.inuker.bluetooth.library.Constants;
//import com.inuker.bluetooth.library.search.SearchResult;
//import com.qztech.bluetooth.BluetoothClientManager;
//import com.qztech.bluetooth.BluetoothConfig;
//import com.qztech.bluetooth.ZBtDevice;
//import com.qztech.bluetooth.event.BleConnectStatusEvent;
//import com.qztech.bluetooth.event.DeviceFoundedEvent;
//import com.qztech.bluetooth.event.SearchStateEvent;
//import com.qztech.sdk.ui.fragment.BaseDrawerFragment;
//
//import org.greenrobot.eventbus.EventBus;
//import org.greenrobot.eventbus.Subscribe;
//import org.greenrobot.eventbus.ThreadMode;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Objects;
//
//import butterknife.BindView;
//import butterknife.ButterKnife;
//import butterknife.OnClick;
//
//@SuppressWarnings("unused")
//public class DeviceFragment extends BaseDrawerFragment {
//    private DeviceAdapter mAdapter;
//
//    @BindView(R.id.btn_refresh)
//    public Button mBtnRefresh;
//    private List<BtDevice> mFoundDevice = new ArrayList<>();
//    @BindView(R.id.lvItems)
//    public ListView mListDevices;
//    @BindView(R.id.pgbLoading)
//    public ProgressBar mLoadingBar;
//
//    @BindView(R.id.radioGroup)
//    RadioGroup mTypeGroup;
//
//    private DViewModel mHomeViewModel;
//    private int mClickCount;
//    private long mLastClickTimeTs;
//
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        EventBus.getDefault().register(this);
//        mHomeViewModel = ViewModelProviders.of(getActivity(), new ViewModelProvider.AndroidViewModelFactory(getActivity().getApplication())).get(DViewModel.class);
//    }
//
//    @Override
//    public void onDestroy() {
//        EventBus.getDefault().unregister(this);
//        super.onDestroy();
//    }
//
//    protected int getResourceId() {
//        return R.layout.fragment_drawer_bluetooth;
//    }
//
//    public void onDestroyView() {
//        mHomeViewModel.cancelSearch();
//        super.onDestroyView();
//    }
//
//    public void onPause() {
//        super.onPause();
//        mHomeViewModel.cancelSearch();
//    }
//
//    public void onRefreshFragment(int paramInt) {
//    }
//
//    public void onResume() {
//        super.onResume();
//        addConnectedDevice();
//    }
//
//    public void onViewCreated(@NonNull View paramView, Bundle paramBundle) {
//        super.onViewCreated(paramView, paramBundle);
//        ButterKnife.bind(this, paramView);
//
//
//        this.mBtnRefresh.setVisibility(View.VISIBLE);
//        this.mFoundDevice.clear();
//
//        this.mAdapter = new DeviceAdapter(getActivity(), this.mFoundDevice);
//        this.mListDevices.setAdapter(this.mAdapter);
//        this.mAdapter.setOnSwichChangedListener((isChecked, btDevice) -> {
//            if (isChecked && !btDevice.isConnected()) {
//                if (getActivity() instanceof CaDspActivity) {
//                    ((CaDspActivity) getActivity()).connect(btDevice.getMac(), btDevice.getName());
//                }
//                return;
//            }
//            DialogHelper.confirm(DeviceFragment.this.getActivity(), R.string.title_device_confirm, R.string.device_message_disconnect, R.string.ok, R.string.cancel, (paramAnonymous2DialogInterface, paramAnonymous2Int) -> {
//                mHomeViewModel.disconnect();
//                mHomeViewModel.sageLastConnectDevice(null);
//                btDevice.setConnected(false);
//                DeviceFragment.this.mAdapter.notifyDataSetChanged();
//            });
//        });
//        mTypeGroup.setOnCheckedChangeListener((group, checkedId) -> {
//            if (checkedId == R.id.rb_source) {
//                BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(DeviceProtocol.setMode(true));
//            } else if (checkedId == R.id.rb_bluetooth) {
//                BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(DeviceProtocol.setMode(false));
//            }
//        });
//    }
//
//    @OnClick({R.id.btn_refresh, R.id.btnResetApp })
//    public void onBtnClick(View view) {
//        int id = view.getId();
//        if (id == R.id.btn_refresh) {
//            refreshList();
//        } else if (id == R.id.btnResetApp) {
//            if (getActivity() instanceof CaDspActivity) {
//                ((CaDspActivity) Objects.requireNonNull(getActivity())).resetAppAsk();
//            }
//        }
//        /*else if (id == R.id.btnContactUs) {
//            if (System.currentTimeMillis() - mLastClickTimeTs > 1000) {
//                mClickCount = 0;
//            }
//            mLastClickTimeTs = System.currentTimeMillis();
//            mClickCount++;
//            if (mClickCount >= 6) {
//                SPUtils spUtils = SPUtils.getInstance(Context.MODE_PRIVATE);
//                spUtils.put(DspContext.KEY_PWD, DspContext.PASSOWRD);
//                Toast.makeText(getContext(), "Password reset!", Toast.LENGTH_SHORT).show();
//                mClickCount = 0;
//            }
//        }*/
//    }
//
//    public void openOrCloseDrawer(int paramInt) {
//        super.openOrCloseDrawer(paramInt);
//    }
//
//    private void addConnectedDevice() {
//        ZBtDevice localBleDevice = mHomeViewModel.getConnectedDevice();
//        if (localBleDevice != null) {
//            BtDevice btDevice = new BtDevice();
//            btDevice.setConnected(true);
//            btDevice.setMac(localBleDevice.getMac());
//            btDevice.setName(localBleDevice.getName());
//            if (!mFoundDevice.contains(btDevice)) {
//                mFoundDevice.add(btDevice);
//            }
//        }
//    }
//
//    public void refreshList() {
//        this.mFoundDevice.clear();
//        addConnectedDevice();
//        if (!BluetoothClientManager.getInstance(getContext()).isBluetoothOpened()) {
//            BluetoothClientManager.getInstance(getContext()).openBluetooth();
//        } else mHomeViewModel.searchBluetooth();
//    }
//
//    public void setUp(int paramInt, DrawerLayout paramDrawerLayout, Toolbar paramToolbar) {
//        super.setUp(paramInt, paramDrawerLayout, paramToolbar);
//        this.mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
//            public void onDrawerClosed(@NonNull View drawerView) {
//                int gravity = ((DrawerLayout.LayoutParams) drawerView.getLayoutParams()).gravity;
//                if (gravity == Gravity.END || gravity == Gravity.RIGHT) {
//                    mHomeViewModel.cancelSearch();
//                    mFoundDevice.clear();
//                    mAdapter.notifyDataSetChanged();
//                }
//            }
//
//            public void onDrawerOpened(@NonNull View drawerView) {
//                int gravity = ((DrawerLayout.LayoutParams) drawerView.getLayoutParams()).gravity;
//                if (gravity == Gravity.END || gravity == Gravity.RIGHT) {
//                    DeviceFragment.this.refreshList();
//                }
//            }
//
//            public void onDrawerSlide(@NonNull View view, float paramAnonymousFloat) {
//            }
//
//            public void onDrawerStateChanged(int paramAnonymousInt) {
//            }
//        });
//    }
//
//    //-----BluetoothEvent---------------
//
//    /**
//     * 发现设备
//     */
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onFoundDevice(DeviceFoundedEvent event) {
//        SearchResult searchResult = event.getDevice();
//        if (!BluetoothConfig.getName().equalsIgnoreCase(searchResult.getName())) return;
//        for (BtDevice btDevice : DeviceFragment.this.mFoundDevice) {
//            if ((btDevice).getMac().equalsIgnoreCase(searchResult.getAddress())) {
//                return;
//            }
//        }
//        BtDevice btDevice = new BtDevice(searchResult.getAddress(), searchResult.getName());
//        if (mFoundDevice.contains(btDevice)) return;
//        mFoundDevice.add(btDevice);
//        mAdapter.notifyDataSetChanged();
//        if (searchResult.getAddress().equals(mHomeViewModel.getLastConnectedDevice())) {
//            ((CaDspActivity) getActivity()).connect(btDevice.getMac(), btDevice.getName());
//        }
//    }
//
//    /**
//     * 搜索事件
//     */
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onSearchEvent(SearchStateEvent event) {
//        switch (event.getStatus()) {
//            case SearchStateEvent.SEARCH_START:
//                mLoadingBar.setVisibility(View.VISIBLE);
//                mBtnRefresh.setEnabled(false);
//                break;
//            case SearchStateEvent.SEARCH_STOP:
//            case SearchStateEvent.SEARCH_CANCEL:
//                mLoadingBar.setVisibility(View.GONE);
//                mBtnRefresh.setEnabled(true);
//                break;
//        }
//    }
//
//    /**
//     * 蓝牙设备连接状态变化
//     */
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onBluetoothStateChanged(BleConnectStatusEvent event) {
//        switch (event.getStatus()) {
//            case Constants.STATUS_DISCONNECTED:
//                for (Object device : mFoundDevice) {
//                    if (device instanceof BtDevice) {
//                        BtDevice bleDevice = (BtDevice) device;
//                        if (bleDevice.getMac().equalsIgnoreCase(event.getDevice().getMac())) {
//                            bleDevice.setConnected(false);
//                            mAdapter.notifyDataSetChanged();
//                            break;
//                        }
//                    }
//                }
//                break;
//            case Constants.STATUS_CONNECTED:
//                for (Object device : mFoundDevice) {
//                    if (device instanceof BtDevice) {
//                        BtDevice bleDevice = (BtDevice) device;
//                        if (bleDevice.getMac().equalsIgnoreCase(event.getDevice().getMac())) {
//                            bleDevice.setConnected(true);
//                            mHomeViewModel.sageLastConnectDevice(bleDevice.getMac());
//                            mAdapter.notifyDataSetChanged();
//                            break;
//                        }
//                    }
//                }
//                break;
//        }
//    }
//}
